package com.imgo.max.temporary.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.imgo.max.common.dto.ResultCodeEnum;
import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.util.HttpClientUtil;
import com.imgo.max.common.util.page.Pagination;
import com.imgo.max.core.tools.DateUtils;
import com.imgo.max.temporary.dao.SuperGirlCarManagerDao;
import com.imgo.max.temporary.dto.ActivityAreaDto;
import com.imgo.max.temporary.dto.SuperGirlCarDto;
import com.imgo.max.user.dto.User;
import com.imgo.max.user.service.UserService;

@Service("SuperGirlCarManagerService")
public class SuperGirlCarManagerServiceImpl implements SuperGirlCarManagerService {

	@Resource SuperGirlCarManagerDao superGirlCarManagerDao;
	
	@Resource  UserService userService;

	@Override
	public ResultDO<List<ActivityAreaDto>> findAllAreas() {
		ResultDO<List<ActivityAreaDto>> result = new ResultDO<List<ActivityAreaDto>>();
		try {
			String str = getAreaJsonData();
			List<ActivityAreaDto> all = new ArrayList<ActivityAreaDto>();
			List<ActivityAreaDto> south = new ArrayList<ActivityAreaDto>();
			List<ActivityAreaDto> north = new ArrayList<ActivityAreaDto>();
			if(StringUtils.isNotBlank(str)){
				int southStartIndex = str.indexOf("[");
				int southEndIndex = str.indexOf("]");
				int northStartIndex = str.lastIndexOf("[");
				int norEndIndex = str.lastIndexOf("]");
				String areaList1 = str.substring(southStartIndex, southEndIndex+1);
				String areaList2 = str.substring(northStartIndex, norEndIndex+1);
				Gson gson=new Gson();
				south = gson.fromJson(areaList1, new TypeToken<List<ActivityAreaDto>>(){}.getType());
				north = gson.fromJson(areaList2, new TypeToken<List<ActivityAreaDto>>(){}.getType());
				all.addAll(south);
				all.addAll(north);
			}
			if(null!=all && all.size()>0){
				for (ActivityAreaDto dto : all) {
					Date startDate = DateUtils.parseDate(dto.getTime_yy_start());
					Date endDate = DateUtils.parseDate(dto.getTime_yy_end());
					Date actDate = DateUtils.parseDate(dto.getTime_act_end());
					Date nowDate = new Date();
					
					if(startDate.after(nowDate)){
						dto.setStatus("未开启预约");
						dto.setFlag(1);
					}else if(nowDate.after(startDate) && nowDate.before(endDate)){
						dto.setStatus("预约中");
						dto.setFlag(2);
					}else if(nowDate.after(endDate) && nowDate.before(actDate)){
						dto.setStatus("预约结束,等待活动开始");
						dto.setFlag(3);
					}else if(nowDate.after(actDate)){
						dto.setStatus("活动结束");
						dto.setFlag(4);
					}
				}
			}
			result.setModule(all);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_FIND_AREAS_ERROR.getResultCode());
			result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_FIND_AREAS_ERROR.getMessage());
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	private String getAreaJsonData() {
		String str = "";
		for (int i = 0; i < 5; i++) {
			str = HttpClientUtil.doGet("http://h5.max.mgtv.com/H5/20160314_mengniu_truck/js/data.js", null);
			if(str.equals("")){
				getAreaJsonData();
			}else{
				break;
			}
		}
		return str;
	}

	@Override
	public ResultDO<Integer> getTotNumByZid(String zid) {
		ResultDO<Integer> resultdo = new ResultDO<Integer>();
		int num = superGirlCarManagerDao.getTotNumByZid(zid);
		resultdo.setModule(num);
		return resultdo;
	}

	@Override
	public ResultDO<List<SuperGirlCarDto>> querySignArtist(String zid, Pagination pagination) {
		ResultDO<List<SuperGirlCarDto>> resultdo = new ResultDO<List<SuperGirlCarDto>>();
		try {
			List<SuperGirlCarDto> dtoList = new ArrayList<SuperGirlCarDto>();
			dtoList = superGirlCarManagerDao.querySignArtist(zid,pagination);
			
			
			List<String> uids = new ArrayList<String>();
			if(null!=dtoList && dtoList.size()>0){
				for (SuperGirlCarDto data : dtoList) {
					if(StringUtils.isNotBlank(data.getUid())){
						uids.add(data.getUid());
					}
				}
			}
			ResultDO<List<User>> usersResultDO = null;
			if(null!=uids && uids.size()>0){
				usersResultDO = userService.getArtist(uids);
			}
			
			if(null!=usersResultDO && null!=usersResultDO.getModule() && usersResultDO.getModule().size()>0){
				ArrayList<User> users = (ArrayList<User>) usersResultDO.getModule();
				Map<String, User> userMap = new HashMap<String, User>();
				for (User user : users) {
					userMap.put(user.getUid(), user);
				}
				
				for (SuperGirlCarDto data : dtoList) {
					if(null!=userMap.get(data.getUid())){
						if(StringUtils.isNotBlank(data.getUid())){
							User user= userMap.get(data.getUid());
							data.setUname(user.getNickName());
							data.setPhoneNum(user.getPhoneNum());
							data.setIdCard(user.getIdentity());
							data.setPhoto(user.getPhoto());
						}
					}
					
				}
			}
			resultdo.setModule(dtoList);
		} catch (Exception e) {
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_FIND_SIGN_ARTIST_ERROR.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_FIND_SIGN_ARTIST_ERROR.getMessage());
			resultdo.setErrorMessage(e.getMessage());
		}
		return resultdo;
	}

	@Override
	public ResultDO<List<SuperGirlCarDto>> queryJoinArtist(String zid) {
		ResultDO<List<SuperGirlCarDto>> resultdo = new ResultDO<List<SuperGirlCarDto>>();
		try {
			List<SuperGirlCarDto> dtoList =  superGirlCarManagerDao.queryJoinArtist(zid);
			List<String> uids = new ArrayList<String>();
			if(null!=dtoList && dtoList.size()>0){
				for (SuperGirlCarDto data : dtoList) {
					if(StringUtils.isNotBlank(data.getUid())){
						uids.add(data.getUid());
					}
				}
			}
			ResultDO<List<User>> usersResultDO = null;
			if(null!=uids && uids.size()>0){
				usersResultDO = userService.getArtist(uids);
			}
			
			if(null!=usersResultDO && null!=usersResultDO.getModule() && usersResultDO.getModule().size()>0){
				ArrayList<User> users = (ArrayList<User>) usersResultDO.getModule();
				Map<String, User> userMap = new HashMap<String, User>();
				for (User user : users) {
					userMap.put(user.getUid(), user);
				}
				
				for (SuperGirlCarDto data : dtoList) {
					if(null!=userMap.get(data.getUid())){
						if(StringUtils.isNotBlank(data.getUid())){
							User user= userMap.get(data.getUid());
							data.setUname(user.getNickName());
							data.setPhoneNum(user.getPhoneNum());
							data.setIdCard(user.getIdentity());
							data.setPhoto(user.getPhoto());
						}
					}
					
				}
			}
			resultdo.setModule(dtoList);
		} catch (Exception e) {
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_FIND_JOIN_ARTIST_ERROR.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_FIND_JOIN_ARTIST_ERROR.getMessage());
			resultdo.setErrorMessage(e.getMessage());
		}
		return resultdo;
	}

	@Override
	public ResultDO<Integer> saveJoinArtist(SuperGirlCarDto dto) {
		ResultDO<Integer> resultdo = new ResultDO<Integer>();
		try {
			
			if(StringUtils.isNotBlank(dto.getUid())){
				List<String> uids = new ArrayList<String>();
				uids.add(dto.getUid());
				ResultDO<List<User>> resuUser = userService.getArtist(uids);
				if(null!=resuUser && null!=resuUser.getModule() && resuUser.getModule().size()>0){
					User user = resuUser.getModule().get(0);
					dto.setUname(user.getNickName());
					dto.setPhoneNum(user.getPhoneNum());
				}
			}
			int flag = superGirlCarManagerDao.saveJoinArtist(dto);
			resultdo.setModule(flag);
		} catch (Exception e) {
			e.printStackTrace();
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_ADD_JOIN_ARTIST_ERROR.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_ADD_JOIN_ARTIST_ERROR.getMessage());
			resultdo.setErrorMessage(e.getMessage());
		}
		return resultdo;
	}

	@Override
	public ResultDO<Integer> deleteJoinArtist(String id) {
		ResultDO<Integer> resultdo = new ResultDO<Integer>();
    	try {
    		int flag = superGirlCarManagerDao.delete(id);
        	resultdo.setModule(flag);
        } catch (Exception e) {
            resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_DELETE_JOIN_ARTIST_ERROR.getResultCode());
            resultdo.setSuccess(false);
        }
		return resultdo;
	}


	

}
